from agentscope.service.service_toolkit import ServiceFunction
from agentscope.service import (
    ServiceToolkit,
    ServiceResponse,
    ServiceExecStatus,
)
import os
def judge(filepath:str, name: str):
    """Call this method to judge the student's solution after they have completed the problem.

    Args:
        filepath (`str`): The path to the file containing the student's solution.
        name (`str`): The name of the problem.

    Returns:
        Report `tuple(int,str)`: The report of the student's solution.
    """
    # 从books/code_tests/name/中获取所有.in文件

    directory = 'books/code_tests/'+name
    # 如果目录不存在，报错
    if not os.path.exists(directory):
        status = ServiceExecStatus.FAILED
        return ServiceResponse(status, f"The name {name} is not found.")
    files = os.listdir(directory)
    in_files = [file for file in files if file.endswith('.in')]
    save_dir = os.path.dirname(filepath)
    Report = ""
    passed_num=0
    failed_num=0
    for file in in_files:
        os.remove(os.path.join(save_dir, f"_{name}.out"))
        cmd = "cat " + os.path.join(directory, file) + " | " + "python " + filepath +" > " + os.path.join(save_dir, f"_{name}.out")
        os.system(cmd)
        # 比较结果
        re = ""
        if compare_file(os.path.join(save_dir, f"_{name}.out"), os.path.join(directory, file.replace(".in", ".out"))):
            re = "Passed {file}"
            passed_num += 1
        else:
            re = "Failed {file}"
            failed_num += 1
        Report = Report + re + "\n"
    os.remove(os.path.join(save_dir, f"_{name}.out"))
    score = int (100 * passed_num / (passed_num + failed_num))
    


    status = ServiceExecStatus.SUCCESS
    return ServiceResponse(status, (Report, score))


def compare_file(path1:str, path2:str):
    """
        Compare two files and return true if they are the same.
    """
    with open(path1, 'r') as f1, open(path2, 'r') as f2:
        # 逐行比较
        for line1, line2 in zip(f1, f2):
            if line1.strip() != line2.strip():
                return False
    return True
        
judge_tools = [judge]